package com.xuanheng.utils.http.async;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;

/**
 * An HttpRequest for a POST request (using form-urlencoded)
 *
 * @author Nikki
 */
public class HttpPostRequest extends HttpRequest {

    public HttpPostRequest(String url) {
        super(url);
    }

    @Override
    public void execute() throws MalformedURLException, IOException, SocketTimeoutException {
        openConnection();

        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        outputStream = connection.getOutputStream();
        if (parameters != null) {
            if (!parameters.isEmpty()) {
                Writer writer = new OutputStreamWriter(outputStream);
                writer.write(parameters.toURLEncodedString());
            }
        }
    }
}
